﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace MvcWeb.Controllers
{
    public partial class HomeController : Controller
    {
        // GET: /Home/
        [Authorize]
        public ActionResult Index()
        {
            ViewBag.UserName = User.Identity.Name;
            return View();
        }

        public ActionResult Login()
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("index");
            }

            return View();
        }

        [HttpPost]
        public ActionResult Login(MvcWeb.ViewModels.Home.LoginModel viewModel)
        {
            if (Membership.ValidateUser(viewModel.UserName, viewModel.Password))
            {
                FormsAuthentication.RedirectFromLoginPage(viewModel.UserName, true);
                return RedirectToAction("index");
            }

            viewModel.Error = "登录名或密码错误";
            return View(viewModel);
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("login");
        }
    }
}
